package com.strebel.meal;

import java.net.URI;
import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.strebel.meal.data.Meal;

public class MealPageAdapter extends PagerAdapter {
	
	private List<Meal> meals;

	@Override
	public int getCount() {
		if(meals != null) {
			return meals.size();
		}
		return 0;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		// TODO Auto-generated method stub
		return view == (View)object;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LayoutInflater mInflater = (LayoutInflater) container.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View view = mInflater.inflate(R.layout.meal_page, null);
		
		Meal meal = meals.get(position);
		
		TextView title = (TextView) view.findViewById(R.id.mealTitle);
		title.setText(meal.getTitle());
		
		ListView ingred = (ListView) view.findViewById(R.id.ingredList);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, meal.getInredients());
		ingred.setAdapter(adapter);
		
		if(meal.getImage() != null) {
			ImageView imageView = (ImageView) view.findViewById(R.id.mealImage);
			imageView.setImageURI(Uri.parse(meal.getImage()));
		}
		
		((ViewPager) container).addView(view, 0);

		return view;
	}
	
	@Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

	public void setMeals(List<Meal> meals) {
		this.meals = meals;
	}
}
